package org.hackystat.projectbrowser.page.settrigger;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.hackystat.projectbrowser.ProjectBrowserSession;
import org.hackystat.projectbrowser.page.setaction.ChooseAction;
import org.hackystat.ambientdevice.server.jaxb.Project;
import org.hackystat.ambientdevice.server.jaxb.Trigger;
import org.hackystat.ambientdevice.server.jaxb.TriggerActionPair;

/**
 * The class list view that lets the user choose a trigger.
 * 
 * @author racesauc(ed)
 * 
 */
public class ChooseTriggerTable extends ListView {

  /** Supports serialization. */
  private static final long serialVersionUID = 1L;

  private List<TriggerActionPair> pairs = new ArrayList<TriggerActionPair>();
  /** Displays text at the bottom of the screen for user information. */
  protected String footerFeedbackk = "";
  private Project project;
  private String tapName;

  /**
   * The table that list all the trigger for this trigger/action pair.
   * 
   * @param id A String passed on to super (not really needed).
   * @param param The page parameters for this page.
   */
  public ChooseTriggerTable(String id, PageParameters param) {
    super(id);

    String projectName = param.getStringValue("projectName").toString();
    project = ProjectBrowserSession.get().getProjectTypeXMLByNameId(projectName);
    tapName = param.getStringValue("tapName").toString();

    List<Trigger> triggers = new ArrayList<Trigger>();
    pairs = project.getTriggerActionPairs().getTriggerActionPair();

    for (TriggerActionPair pair : pairs) {
      if (pair.getId().equals(tapName)) {
        if (pair.getTriggers() == null) {
          System.out.println("There are no triggers.");
        }

        else {
          triggers.addAll(pair.getTriggers().getTrigger());
          setList(triggers);
        }
      }
    }
  }

  /**
   * Populate the table with item
   * 
   * @param item The items to populate the table with.
   */
  @Override
  protected void populateItem(ListItem item) {
    PageParameters param = new PageParameters();
    Trigger trigger = (Trigger) item.getModelObject();
    param.put("triggerName", trigger.getId());
    System.out.println("In choose tap table" + trigger.getId());
    item.add(new Label("triggerName", trigger.getId()));
    // FOR NOW POINT TO THE SET TRIGGER PAGE BECAUSE WE HAVE NO PAGE TO POINT TO

    PageParameters param2 = new PageParameters();
    param2.put("projectName", project.getProjectName());
    item.add(new BookmarkablePageLink("projectLink", SetTriggerPage.class, param2));

    PageParameters param3 = new PageParameters();
    param3.put("projectName", project.getProjectName());
    param3.put("tapName", tapName);
    param3.put("oldTriggerName", trigger.getId());
    item.add(new BookmarkablePageLink("GetActions", ChooseAction.class, param3));
    item.add(new BookmarkablePageLink("EditTriggerName", EditTriggerName.class, param3));
    // item.add(new BookmarkablePageLink("NewTriggerName", NewTriggerName.class, param3));
    item.add(new DeleteTriggerLink("deleteTrigger", trigger.getId(), project.getProjectName(),
        tapName));
  }

}
